<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="序号" prop="orderNum">
          <el-input
            v-model="queryParams.orderNum"
            placeholder="请输入序号模糊查询"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="状态" prop="sysNormalDisable">
          <el-select v-model="queryParams.sysNormalDisable"  placeholder="请选择状态" clearable >
            <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="人员分组" prop="comnDictUserGroup">
          <el-select v-model="queryParams.comnDictUserGroup"  placeholder="请选择人员分组" clearable >
            <el-option
              v-for="dict in dict.type.comn_dict_user_group"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="人员姓名" prop="nickName">
          <el-select
            filterable
            v-model="queryParams.nickName"
            placeholder="请选择人员姓名"
            clearable
          >
            <el-option
              v-for="item in nickNameList"
              :key="item.userId"
              :label="item.nickName"
              :value="item.nickName"
            />
          </el-select>
        </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['comn:userGroup:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['comn:userGroup:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['comn:userGroup:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['comn:userGroup:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" stripe :data="userGroupList" @selection-change="handleSelectionChange" :highlight-current-row="true">
      <el-table-column type="selection" width="50" align="center" fixed="left"/>
      <el-table-column type="expand" width="80">
        <template slot-scope="props">
          <el-form label-position="left" class="demo-table-expand">
            <el-form-item label="备注：">
              <span>{{ props.row.docNote }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="序号" align="center" prop="orderNum" width="80" />
      <el-table-column label="人员分组" align="center" prop="comnDictUserGroup" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.comn_dict_user_group" :value="scope.row.comnDictUserGroup"/>
        </template>
      </el-table-column>
      <el-table-column label="人员姓名" align="center" prop="nickName" />
      <el-table-column label="状态" align="center" prop="sysNormalDisable">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.sysNormalDisable"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['comn:userGroup:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['comn:userGroup:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改人员分组对话框 -->
    <el-dialog v-dialogDrag v-dialogDragWidth :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal=false>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="序号" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入序号" :style="{width: '100%'}" clearable/>
        </el-form-item>

        <el-form-item label="人员分组" prop="comnDictUserGroup">
          <el-select v-model="form.comnDictUserGroup" clearable filterable placeholder="请选择人员分组" :style="{width: '100%'}">
            <el-option
              v-for="dict in dict.type.comn_dict_user_group"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="人员姓名" prop="fkUserId">
          <el-input v-model="form.fkUserId"
                    v-show="false"
                    :style="{width: '100%'}"
          />
          <el-input placeholder="请点击右侧按钮选择人员姓名"  readOnly="true" v-model="form.nickName" class="input-with-select">
            <el-button type="info" plain @click="dialog_show_selectUser" slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-form-item>

        <el-form-item label="状态" prop="sysNormalDisable">
          <el-select v-model="form.sysNormalDisable" clearable filterable placeholder="请选择状态" :style="{width: '100%'}">
            <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="备注" prop="docNote">
          <el-input v-model="form.docNote" placeholder="请输入备注" :style="{width: '100%'}" type="textarea" autosize/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <select-user ref="component_selectUser" :roleId="queryParams.roleId" @ok="selectUser_done"/>

  </div>
</template>

<script>
import { listUserGroup, getUserGroup, delUserGroup, addUserGroup, updateUserGroup } from "@/api/comn/userGroup";
import selectUser from "../../system/user/selectUser";
import { listUser} from "@/api/system/user";
export default {
  name: "UserGroup",
  components: { selectUser },
  dicts: ['comn_dict_user_group', 'sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员分组表格数据
      userGroupList: [],
      //人员姓名下拉
      nickNameList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        comnDictUserGroup: null,
        orderNum: null,
        fkUserId: null,
        sysNormalDisable: null,
        docTitle: null,
        docNum: null,
        docDt: null,
        docNote: null,
        confirmDt: null,
        oaDictConfirmResult: null,
        confirmNote: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        comnDictUserGroup: [
          { required: true, message: "人员分组不能为空", trigger: "change" }
        ],
        fkUserId: [
          { required: true, message: "人员姓名不能为空", trigger: "change" }
        ],
        sysNormalDisable: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getList_nickName();
  },
  methods: {
    /** 查询人员分组列表 */
    getList() {
      this.loading = true;
      listUserGroup(this.queryParams).then(response => {
        this.userGroupList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getList_nickName(){
      listUser().then(response => {
        this.nickNameList = response.rows
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userGroupUuid: null,
        comnDictUserGroup: null,
        orderNum: null,
        fkUserId: null,
        sysNormalDisable: '0',
        docTitle: null,
        docNum: null,
        nickName:null,
        docDt: null,
        docNote: null,
        confirmDt: null,
        oaDictConfirmResult: null,
        confirmNote: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userGroupUuid)
      this.nickNames = selection.map(item => item.nickName)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加人员分组";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userGroupUuid = row.userGroupUuid || this.ids
      getUserGroup(userGroupUuid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改人员分组";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userGroupUuid != null) {
            updateUserGroup(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUserGroup(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userGroupUuids = row.userGroupUuid || this.ids;
      const nickNames = row.nickName || this.nickNames;
      this.$modal.confirm('是否确认删除人员姓名为"' +nickNames + '"的数据项？','删除确认').then(function() {
        return delUserGroup(userGroupUuids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('comn/userGroup/export', {
        ...this.queryParams
      }, `comn_userGroup.xlsx`)
    },
 /** 打开授权用户表弹窗 */
 dialog_show_selectUser() {
  this.$refs.component_selectUser.show();
},

/** 选择用户完成 */
selectUser_done(user) {
  this.form.fkUserId = user.userId
  this.form.nickName = `${user.nickName}（用户名：${user.userName}，ID：${user.userId}）`
},
  }
};
</script>
